﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;

namespace WeakEndForum
{
    public partial class EditMessage : System.Web.UI.Page
    {
        int mID = 0;
        string getURIParam = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Modules.isRequestedQueryStringValid("mID", "ID", "Messages", out getURIParam))
            {
                mID = Convert.ToInt32(getURIParam);
            }
            else
            {
                Server.Transfer("Error404.aspx");
            }
            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand isUserValid = new OleDbCommand("Select Auteur FROM Messages WHERE Messages.ID = @mID ;", conn);
            isUserValid.Parameters.Add(new OleDbParameter("mID", mID) { OleDbType = OleDbType.Integer });
            isUserValid.Prepare();
            OleDbDataReader dataReader = isUserValid.ExecuteReader();
            if (dataReader.Read())
            {
                if (Modules.getConnectedUser() != (string)dataReader[0])
                {
                    Server.Transfer("Error403.aspx");
                }
            }
            conn.Close();
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            {
                mID = Convert.ToInt32(getURIParam);
                OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
                conn.Open();
                OleDbCommand getMessage = new OleDbCommand("SELECT Contenu FROM Messages WHERE Messages.ID = @mID ;", conn);
                getMessage.Parameters.Add(new OleDbParameter("mID", mID) { OleDbType = OleDbType.Integer });
                getMessage.Prepare();
                OleDbDataReader dataReader = getMessage.ExecuteReader();
                if (dataReader.Read())
                {
                    txtEditMessageContent.Text = (string)dataReader[0];
                }
                conn.Close();
            }
        }

        protected void btnEditMessage_Click(object sender, EventArgs e)
        {
            string updatedMessage = txtEditMessageContent.Text;
            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings[Constantes.DB_NAME].ConnectionString);
            conn.Open();
            OleDbCommand EditMessage = new OleDbCommand("UPDATE Messages SET Contenu = @Contenu WHERE ID = @ID;", conn);
            EditMessage.Parameters.Add(new OleDbParameter("Contenu", updatedMessage) { OleDbType = OleDbType.LongVarChar, Size = 65535 });
            EditMessage.Parameters.Add(new OleDbParameter("ID", mID) { OleDbType = OleDbType.Integer });
            EditMessage.Prepare();
            EditMessage.ExecuteNonQuery();

            OleDbCommand Redirect = new OleDbCommand("SELECT Sujet FROM Messages WHERE ID=@id;", conn);
            Redirect.Parameters.Add(new OleDbParameter("id", mID) { OleDbType = OleDbType.VarChar, Size = 255 });
            Redirect.Prepare();
            OleDbDataReader dataReader = Redirect.ExecuteReader();
            string redirectURL = "Topic.aspx?tID=";    
            if (dataReader.Read())
            {
                redirectURL += dataReader[0].ToString();
            }
            conn.Close();
            Server.Transfer(redirectURL);
            
        }
    }
}